import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login', name: 'Login' },
  { path: '/login', name: 'login', component: Login },
  { path: '/home', name: 'Home', component: Home }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  } else {
    const token = window.sessionStorage.getItem('token')
    if (token) {
      return next()
    } else {
      return next('/login')
    }
  }
})

export default router
